import 'package:flutter/material.dart';
import 'package:meePet/app/LocalColor.dart';
import 'package:meePet/base/BaseFragment.dart';
import 'package:meePet/utils/ScreenUtils.dart';
import 'package:meePet/utils/ext/SizeExt.dart';

import '../../bean/msg/MsgData.dart';
import 'item/Conversation.dart';

class ChatFragment extends BaseFragment {
  @override
  State<StatefulWidget> createState() {
    return ChatFragmentState();
  }
}

class ChatFragmentState extends BaseFragmentState<ChatFragment> {
  late ConversationItem conversationItem;
  List<ConversationMsg> dataList = [
    ConversationMsg(),
    ConversationMsg(),
    ConversationMsg(),
    ConversationMsg()
  ];

  @override
  void onCreate() {
    conversationItem = ConversationItem();
    super.onCreate();
  }

  @override
  Widget initView(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
        top: MediaQuery.of(context).padding.top / 2,
        bottom: 0,
      ),
      child: _rootLayout(context),
    );
  }

  ///根布局
  Widget _rootLayout(BuildContext context) {
    return Stack(
      children: [
        const SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: DecoratedBox(
              decoration: BoxDecoration(color: LocalColor.color_FFFAFAFA)),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            _getTittleBar(),
            Expanded(
                child: PhysicalModel(
              borderRadius: BorderRadius.circular(10.sw()),
              color: Colors.transparent,
              clipBehavior: Clip.hardEdge,
              child: _conversationListWidget(context, dataList),
            ))
          ],
        ),
      ],
    );
  }

  Widget _getTittleBar() {
    return Stack(
      children: [
        SizedBox(
          width: ScreenUtils.screenWidth,
          height: ScreenUtils.statusBarHeight / 2 + 40.sw(),
          child: DecoratedBox(
              decoration: BoxDecoration(color: LocalColor.colorWhite)),
        ),
        Container(
          margin: EdgeInsets.only(top: ScreenUtils.statusBarHeight / 2),
          child: Row(
            children: [
              Container(
                  padding: EdgeInsets.only(left: 15.sw()), child: Text("添加")),
              Expanded(
                  child: SizedBox(
                height: 40.sw(),
                width: 0,
                child: Align(
                  child: Text("会话列表"),
                  alignment: Alignment.center,
                ),
              )),
              Container(
                  padding: EdgeInsets.only(right: 15.sw()), child: Text("+")),
            ],
          ),
        )
      ],
    );
  }

  Widget _conversationListWidget(
      BuildContext context, List<ConversationMsg> list) {
    return ListView.separated(
      padding: EdgeInsets.only(top: 8.sw(), left: 8.sw(), right: 8.sw()),
      itemBuilder: (context, index) => conversationItem.getMsgItem(list[index]),
      separatorBuilder: (context, index) => Divider(
        height: 1.sw(),
        color: LocalColor.colorWhite,
      ),
      itemCount: list.length,
    );
  }
}
